{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-11-14T08:52:41.589510800Z",
     "start_time": "2023-11-14T08:52:41.574759600Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "目前服务银行是 中国银行\n",
      "银行服务理念是 以客为尊\n"
     ]
    }
   ],
   "source": [
    "class Banks():\n",
    "    # 定义银行类别\n",
    "    title = \"中国银行\"          # 定义属性\n",
    "    def motto(self):           # 定义方法\n",
    "        return \"以客为尊\"\n",
    "\n",
    "userbank = Banks()\n",
    "print(\"目前服务银行是\",userbank.title)\n",
    "print(\"银行服务理念是\",userbank.motto())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hung 存款余额是: 10000\n"
     ]
    }
   ],
   "source": [
    "class Banks():\n",
    "    title = \"农业银行\"\n",
    "    def __init__(self,uname,money):\n",
    "        self.name = uname\n",
    "        self.balance = money\n",
    "\n",
    "    def get_balance(self):\n",
    "        return self.balance\n",
    "\n",
    "hungbank = Banks(\"Hung\",10000)\n",
    "print(hungbank.name,\"存款余额是:\",hungbank.get_balance())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-14T09:19:52.133037800Z",
     "start_time": "2023-11-14T09:19:52.124559200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hung 存款余额是: 10000\n",
      "存款 1000 完成\n",
      "Hung 存款余额是: 11000\n",
      "提款 1000 完成\n",
      "Hung 存款余额是: 10000\n"
     ]
    }
   ],
   "source": [
    "class Banks():\n",
    "    title = \"农业银行\"       # 定义属性\n",
    "    def __init__(self,uname,money):       # 初始化方法\n",
    "        self.name = uname                 # 设定存款者名称\n",
    "        self.balance = money              # 设定所存的钱\n",
    "\n",
    "    def save_money(self,money):           # 设计存款方法\n",
    "        self.balance += money             # 执行存款\n",
    "        print(f\"存款 {money} 完成\")        # 打印存款完成\n",
    "\n",
    "    def withdraw_money(self,money):       # 设计提款方法\n",
    "        self.balance -= money             # 执行提款\n",
    "        print(f\"提款 {money} 完成\")         # 打印提款完成\n",
    "\n",
    "    def get_balance(self):                # 获得存款余额\n",
    "        print(self.name,\"存款余额是:\",self.balance)\n",
    "\n",
    "\n",
    "hungbank = Banks(\"Hung\",10000)\n",
    "hungbank.get_balance()\n",
    "hungbank.save_money(1000)     # 存款1000\n",
    "hungbank.get_balance()\n",
    "hungbank.withdraw_money(1000) # 提款1000\n",
    "hungbank.get_balance()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-14T09:39:30.368174800Z",
     "start_time": "2023-11-14T09:39:30.350323600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "目前开户银行 农业银行\n",
      "Hung 存款余额是: 0\n",
      "存款 100 完成\n",
      "Hung 存款余额是: 100\n"
     ]
    }
   ],
   "source": [
    "class Banks():\n",
    "    def __init__(self,uname):       # 初始化方法\n",
    "        self.name = uname                 # 设定存款者名称\n",
    "        self.balance = 0              # 设定所存的钱\n",
    "        self.title = \"农业银行\"\n",
    "\n",
    "    def save_money(self,money):           # 设计存款方法\n",
    "        self.balance += money             # 执行存款\n",
    "        print(f\"存款 {money} 完成\")        # 打印存款完成\n",
    "\n",
    "    def withdraw_money(self,money):       # 设计提款方法\n",
    "        self.balance -= money             # 执行提款\n",
    "        print(f\"提款 {money} 完成\")         # 打印提款完成\n",
    "\n",
    "    def get_balance(self):                # 获得存款余额\n",
    "        print(self.name,\"存款余额是:\",self.balance)\n",
    "\n",
    "\n",
    "hungbank = Banks(\"Hung\")\n",
    "print(\"目前开户银行\",hungbank.title)\n",
    "hungbank.get_balance()\n",
    "hungbank.save_money(100)\n",
    "hungbank.get_balance()\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-14T09:44:46.813096400Z",
     "start_time": "2023-11-14T09:44:46.807115900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "This is a public method\n"
     ]
    }
   ],
   "source": [
    "class MyClass:\n",
    "    def __init__(self):\n",
    "        self.public_var = 10\n",
    "\n",
    "    def public_method(self):\n",
    "        return \"This is a public method\"\n",
    "\n",
    "obj = MyClass()\n",
    "print(obj.public_var)       # 可以访问公有属性\n",
    "print(obj.public_method())  # 可以访问公有方法\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-15T08:38:23.509776200Z",
     "start_time": "2023-11-15T08:38:23.499778700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n",
      "This is a private method\n"
     ]
    }
   ],
   "source": [
    "class MyClass:\n",
    "    def __init__(self):\n",
    "        # 构造函数，初始化私有属性\n",
    "        self.__private_var = 20\n",
    "\n",
    "    def __private_method(self):\n",
    "        # 私有方法\n",
    "        return \"This is a private method\"\n",
    "\n",
    "obj = MyClass()\n",
    "# 创建对象\n",
    "# 在外部访问私有属性和方法\n",
    "print(obj._MyClass__private_var)       # 使用名称重整方式访问私有属性\n",
    "print(obj._MyClass__private_method())  # 使用名称重整方式访问私有方法"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-15T09:16:07.251837900Z",
     "start_time": "2023-11-15T09:16:07.238428300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x: 10, y: 20\n",
      "30\n",
      "x: 5, y: 10, z: 15\n",
      "30\n"
     ]
    }
   ],
   "source": [
    "class ParentClass:\n",
    "    def __init__(self, x, y):\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "\n",
    "    def print_values(self):\n",
    "        print(f\"x: {self.x}, y: {self.y}\")\n",
    "\n",
    "    def calculate_sum(self):\n",
    "        return self.x + self.y\n",
    "\n",
    "\n",
    "class ChildClass(ParentClass):   # 父类（ParentClass）：子类继承的父类\n",
    "    def __init__(self, x, y, z):\n",
    "        super().__init__(x, y)    # 调用父类的初始化方法，传入参数x和y\n",
    "        self.z = z\n",
    "\n",
    "    def print_values(self):\n",
    "        print(f\"x: {self.x}, y: {self.y}, z: {self.z}\")\n",
    "\n",
    "    def calculate_sum(self):\n",
    "        return self.x + self.y + self.z\n",
    "\n",
    "# 调用父类方法\n",
    "parent_obj = ParentClass(10, 20)\n",
    "parent_obj.print_values()  # 调用父类的方法\n",
    "sum_value = parent_obj.calculate_sum()  # 调用父类的方法\n",
    "print(sum_value)  # 输出父类方法返回的值\n",
    "\n",
    "# 调用子类方法\n",
    "child_obj = ChildClass(5, 10, 15)\n",
    "child_obj.print_values()  # 调用子类的方法\n",
    "sum_value = child_obj.calculate_sum()  # 调用子类的方法\n",
    "print(sum_value)  # 输出子类方法返回的值"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-15T12:52:14.013111900Z",
     "start_time": "2023-11-15T12:52:13.999105100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "class MyClass:\n",
    "    pass\n",
    "\n",
    "obj = MyClass()\n",
    "\n",
    "# 检查 obj 是否是 MyClass 的实例\n",
    "result = isinstance(obj, MyClass)\n",
    "print(result)  # 输出 True\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-15T15:18:17.742915800Z",
     "start_time": "2023-11-15T15:18:17.706154100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是一个示例类的文档字符串。\n"
     ]
    }
   ],
   "source": [
    "class MyClass:\n",
    "    \"\"\"这是一个示例类的文档字符串。\"\"\"\n",
    "\n",
    "print(MyClass.__doc__)  # 输出：这是一个示例类的文档字符串。"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-15T15:49:56.705177200Z",
     "start_time": "2023-11-15T15:49:56.646502800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MyClass object with value: 10\n",
      "MyClass(10)\n"
     ]
    }
   ],
   "source": [
    "class MyClass:\n",
    "    def __init__(self, value):\n",
    "        self.value = value\n",
    "\n",
    "    def __str__(self):\n",
    "        return f\"MyClass object with value: {self.value}\"\n",
    "\n",
    "    def __repr__(self):\n",
    "        return f\"MyClass({self.value})\"\n",
    "\n",
    "obj = MyClass(10)\n",
    "print(str(obj))   # 输出：MyClass object with value: 10\n",
    "print(repr(obj))  # 输出：MyClass(10)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-15T15:59:13.490446700Z",
     "start_time": "2023-11-15T15:59:13.472440Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "class MyIterator:\n",
    "    def __init__(self, max_val):\n",
    "        self.max_val = max_val\n",
    "        self.curr_val = 0\n",
    "\n",
    "    def __iter__(self):\n",
    "        return self\n",
    "\n",
    "    def __next__(self):\n",
    "        if self.curr_val < self.max_val:\n",
    "            self.curr_val += 1\n",
    "            return self.curr_val\n",
    "        else:\n",
    "            raise StopIteration\n",
    "\n",
    "my_iter = MyIterator(5)\n",
    "for val in my_iter:\n",
    "    print(val)  # 输出：1 2 3 4 5\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-15T16:02:31.344982200Z",
     "start_time": "2023-11-15T16:02:31.324648200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
